export function getImgSrc(richtext, num = 3) {
	let imgList = [];
	richtext.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/g, (match, capture) => {
		imgList.push(capture);
	});
	imgList = imgList.slice(0, num);
	return imgList;
}

//向外导出省份
export function getProvince() {
	return new Promise((resolve, reject) => {
		let historyProvince = uni.getStorageSync("historyProvince");
		if (historyProvince) {
			if (Date.now() - historyProvince.time > 1000 * 60 * 60) {
				getIp()
					.then((res) => {
						resolve(res);
					})
					.catch((err) => {
						reject(err);
					});
			} else {
				resolve(historyProvince.province);
			}
		} else {
			getIp()
				.then((res) => {
					resolve(res);
				})
				.catch((err) => {
					reject(err);
				});
		}
	});
}

//获取所在省市
function getIp() {
	return new Promise((resolve, reject) => {
		uni.request({
			url: "https://restapi.amap.com/v3/ip?Key=0c807e057e6b17db95b453dbdd25a825",
			success: (res) => {
				let str = "";
				typeof res.data.province == "string" ?
					(str = res.data.province) :
					(str = "火星");
				resolve(str);
				let obj = {
					province: str,
					time: Date.now(),
				};
				uni.setStorageSync("historyProvince", obj);
			},
			fail: (err) => {
				reject(err);
			},
		});
	});
}
// 获取昵称
export function getName(item) {
	return (
		item?.user_id[0].nickname ||
		item?.user_id[0].username ||
		item?.user_id[0].mobile ||
		"请设置昵称"
	);
}

// // 获取头像
export function giveAvatar(item) {
	return (
		item?.user_id[0]?.avatar_file?.url ?? "../../static/images/user-default.jpg"
	);
}

//点赞操作数据库的方法
export async function likeFun(article_id) {
	console.log("88888888888888888888888888888888888888888")
	let count = await db.collection("quanzi_like").where(
		`article_id=="${article_id}"&& user_id==$cloudEnv_uid`).count()
	console.log("点赞记录里面有：", count)
	if (count.result.total) {
		//说明已经点了赞
		console.log("点了取消赞")
		db.collection("quanzi_like").where(`article_id=="${article_id}" && user_id==$cloudEnv_uid`)
			.remove().then(res => {
				console.log("成功取消点赞", res)
				utilsObj.opertion("quanzi_article", "like_count", article_id, -1)
			}).catch(err => {
				console.log("失败点了取消赞", err)
			})
	} else {
		console.log("点赞加一")
		db.collection("quanzi_like").add({
			article_id: article_id
		}).then(res => {
			console.log("成功点赞", res)
			utilsObj.opertion("quanzi_article", "like_count", article_id, 1)
		}).catch(err => {
			console.log("失败点赞", err)
		})
	}


}